<?php
namespace app\system\controller;
use think\Db;
use app\common\model\Account;
use app\common\model\Logs;
class Finance extends Common
{
    protected $pageSize = 20;
    protected $table = '';

    function select(){
        $where = ['t.is_del'=>'0'];
        if(input('keyword')) $where['t.code'] = ['like','%'.input('keyword').'%'];
        if(input('start_time')) $where['t.create_time'] = ['>=',strtotime(input('start_time'))];
        if(input('end_time')) $where['t.create_time'] = ['<=',strtotime(input('end_time').' +1 day')];
        if(input('start_time') && input('end_time')) $where['t.create_time'] = ['between',[strtotime(input('start_time')),strtotime(input('end_time').' +1 day')]];

        return $list = db($this->table.' t')
        ->join('user u','t.user_id=u.id')
        ->field('t.*,u.code')
        ->where($where)->order('t.id desc');
    }

    function index(){
        
        
        return $this->fetch();
    }
    /**
     * 列表
     * @param string $keywords
     * @return mixed
     */
    public function remit($keywords='')
    {
        $this->table='recharge';
        $list = $this->select()->paginate(20, false, ['query' => request()->get()]);
        $this->assign('list',$list);
        return $this->fetch();
    }
    /**
     * 审核充值
     *
     * @param Type|null $var
     * @return void
     * @author 心如止水
     * @date 2021-11-18
     */
    public function remit_do($id,$state)
    {
        Db::startTrans();
        $info = db('recharge')->find($id);
        if (empty($info)) $this->jsonError('审核失败');
        if($info['state'] != 0){
            $this->jsonError('此笔记录已审核，请勿重复审核');
        }
        if (empty($info)) return json(['status' => -1, 'message' => '审核失败']);
        if ($state == 1 && $info['state']==0) {
            Account::add($info['user_id'], 'bonus1', $info['amount'], '充值', '+');
            Logs::adds('审核充值，记录ID【' . $id . '】', $this->admin['id'], $this->admin['AdminName']);
        }
        $update=[
            'admin_id'=>$this->admin['id'],
            'admin_name'=>$this->admin['username'],
            'state'=>$state,
            'confirm_time'=>time(),
        ];
        $res=db('recharge')->where(['id'=>$id])->update($update);
        Db::commit();
        return $this->jsonSuccess('已审核');
    }
    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function del($id)
    {
        if (isAjaxPost()) {
            $id = intval($id);
            if ($id <= 0) {
                return $this->jsonError('删除失败');
            }

            $model = new \app\common\model\Shop();
            if ($model->where(['id'=>$id])->delete() !== false) {
                return $this->jsonSuccess('删除成功');
            } else {
                return $this->jsonError('删除失败');
            }
        }
    }

    /**
     * 添加/编辑
     * @param int $id
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save($id=0) {
        $model = new \app\common\model\Shop();
        if (isAjaxPost()) {
            $data = input('post.');
            $validate = new \app\system\validate\Shop();
            if (!$validate->check($data)) {
                return $this->jsonError($validate->getError());
            }

            unset($data['id']);
            if ($id > 0) {
                if ($model->update($data, ['id'=>$id]) !== false) {
                    return $this->jsonSuccess('修改成功');
                } else {
                    return $this->jsonError('修改失败');
                }
            } else {
                if ($model->insert($data)) {
                    return $this->jsonSuccess('添加成功');
                } else {
                    return $this->jsonError('添加失败');
                }
            }
        }

        if ($id > 0) {
            $this->assign('data', $model->where(['id'=>$id])->find());
        }

        return $this->fetch();
    }

    /**
     * 批量删除
     * */
    public function delAll()
    {
        if (isAjaxPost()) {
            $post = input('post.');

            if (empty($post['id']) || !is_array($post['id']) || count($post['id']) <= 0) {
                return $this->jsonError('请勾选需要删除的用户！');
            }
            $ids = $post['id'];

            $model = new \app\common\model\Shop();
            if ($model->where(['id'=>['in', $ids]])->delete() !== false) {
                return $this->jsonSuccess('删除成功');
            } else {
                return $this->jsonError('删除失败');
            }
        }
    }
}